package org.ens.delegation.dao.impl;

import java.util.List;
import org.ens.delegation.bo.Utilisateur;
import org.ens.delegation.dao.IUtilisateurDao;
import org.hibernate.Criteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;

public class UtilisateurDao extends GenericDao<Utilisateur, Integer> implements IUtilisateurDao {

    @Override
    public Class<Utilisateur> getReferencedClass() {
        return Utilisateur.class;
    }

    @Override
    public List<Utilisateur> findByFilters(String orderCol, String orderDir, String username, String nom, String prenom, String idRole) {

        Criteria criteria = getHibernateTemplate().getSessionFactory().getCurrentSession().createCriteria(getReferencedClass());

        Order o;
        if (orderDir.equals("asc")) {
            o = Order.asc(orderCol);
        } else {
            o = Order.desc(orderCol);
        }

        if (username != null && !username.isEmpty()) {
            criteria.add(Restrictions.like("username", username));
        }

        if (nom != null && !nom.isEmpty()) {
            criteria.add(Restrictions.like("nom", nom));
        }

        if (prenom != null && !prenom.isEmpty()) {
            criteria.add(Restrictions.like("prenom", prenom));
        }

        if (idRole != null && !idRole.isEmpty()) {
            criteria.add(Restrictions.eq("role.id", new Integer(idRole)));
        }

        criteria.addOrder(o);
        return criteria.list();

    }

    @Override
    public List<Utilisateur> findByFiltersWithFirstMaxResult(int first, int max, String orderCol, String orderDir, String username, String nom, String prenom, String idRole) {

        Criteria criteria = getHibernateTemplate().getSessionFactory().getCurrentSession().createCriteria(getReferencedClass());

        Order o;
        if (orderDir.equals("asc")) {
            o = Order.asc(orderCol);
        } else {
            o = Order.desc(orderCol);
        }

        if (username != null && !username.isEmpty()) {
            criteria.add(Restrictions.like("username", username));
        }

        if (nom != null && !nom.isEmpty()) {
            criteria.add(Restrictions.like("nom", nom));
        }

        if (prenom != null && !prenom.isEmpty()) {
            criteria.add(Restrictions.like("prenom", prenom));
        }

        if (idRole != null && !idRole.isEmpty()) {
            criteria.add(Restrictions.eq("role.id", new Integer(idRole)));
        }

        criteria.setFirstResult(first);
        criteria.setMaxResults(max);

        criteria.addOrder(o);
        return criteria.list();
    }

    @Override
    public Integer getRowCountByFilters(String username, String nom, String prenom, String idRole) {

        Criteria criteria = getHibernateTemplate().getSessionFactory().getCurrentSession().createCriteria(getReferencedClass());

        if (username != null && !username.isEmpty()) {
            criteria.add(Restrictions.like("username", username));
        }

        if (nom != null && !nom.isEmpty()) {
            criteria.add(Restrictions.like("nom", nom));
        }

        if (prenom != null && !prenom.isEmpty()) {
            criteria.add(Restrictions.like("prenom", prenom));
        }

        if (idRole != null && !idRole.isEmpty()) {
            criteria.add(Restrictions.eq("role.id", new Integer(idRole)));
        }

        return (Integer) criteria.uniqueResult();
    }
}
